<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
      class="globalSerchFormStyle"
    >
      <el-row>
        <el-col :span="6">
          <el-form-item label="活动名称" prop="activityName">
            <el-input
              v-model="queryParams.activityName"
              placeholder="请输入活动名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
              style="width: 190px"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="活动类型" prop="activityType">
            <el-select
              v-model="queryParams.activityType"
              placeholder="请选择活动类型"
              clearable
              size="small"
              style="width: 190px"
            >
              <el-option label="按固定金额" value="RFA" />
              <el-option label="按固定数量" value="LOR" />
              <el-option label="每升立减" value="AAL" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="global_el_rowStyle">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
          type="primary"
          plain
          >重置</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button plain icon="el-icon-plus" size="mini" @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5" v-if="$store.state.user.userLevel === '1'">
        <el-button
          plain
          icon="el-icon-s-tools"
          size="mini"
          @click="handleReductionConfig"
          >配置</el-button
        >
      </el-col>
      <!-- <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar> -->
    </el-row>

    <el-table
      stripe
      v-loading="loading"
      :data="reductionActivityList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="left" />
      <el-table-column label="活动名称" align="left" prop="activityName" />
      <el-table-column label="活动用途" align="left" prop="activityUse" />
      <el-table-column label="活动类型" align="left" prop="activityType" />
      <el-table-column label="活动方式" align="left" prop="activityMode" />
      <el-table-column label="会员等级" align="left" prop="memberLevel">
        <!-- <template slot-scope="{ row }">{{ formatArray(row.memberLevels) }}</template> -->
      </el-table-column>
      <el-table-column label="支付方式" align="left" >
        <template slot-scope="{ row }">
          {{ formatArray(row) }}
          <el-tooltip :content="formatTip(row)" placement="top" effect="light"
            v-if="(row.cardType && row.cardType.length) || (row.physicalCardType && row.physicalCardType.length)">
            <el-button type="text" icon="el-icon-more" style="color: grey;"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column
        label="活动开始时间"
        align="left"
        prop="startDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDate, "{y}-{m}-{d} {h}:{i}:{s}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="活动截止时间"
        align="left"
        prop="endDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, "{y}-{m}-{d} {h}:{i}:{s}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="left" prop="createUserName" />
      <el-table-column label="修改人" align="left" prop="modifyUserName" />
      <el-table-column
        label="修改时间"
        align="left"
        prop="modifyTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.modifyTime, "{y}-{m}-{d} {h}:{i}:{s}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="left" prop="activityEnabled">
        <template slot-scope="{ row }">
          <span v-if="row.activityEnabled === '0'">未启用</span>
          <span v-if="row.activityEnabled === '1'">进行中</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        fixed="right"
        width="200"
        align="left"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >查看</el-button
          >
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          > -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleEdit(scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="text"
            :style="{
              color: scope.row.activityEnabled === '1' ? 'red' : '#F59A23',
            }"
            @click="updateStatus(scope.row)"
            >{{
              scope.row.activityEnabled === "1" ? "停止" : "启动"
            }}</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 立减配置 -->
    <ReductionConfig
      :reductionConfigDialog.sync="reductionConfigDialog"
    ></ReductionConfig>
  </div>
</template>

<script>
import {
  listReductionActivity,
  getReductionActivity,
  delReductionActivity,
  addReductionActivity,
  updateReductionActivity,
  exportReductionActivity,
  updateStatus,
} from "@/api/market/reductionActivity";
import ReductionConfig from "./reduction_config";

export default {
  name: "ReductionActivity",
  components: { ReductionConfig },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 立减活动表格数据
      reductionActivityList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        activityName: null,
        activityUse: null,
        activityType: null,
        activityMode: null,
        oilPartOrAll: null,
        memberCondition: null,
        memberLevels: null,
        paymentMethod: null,
        startDate: null,
        endDate: null,
        maxCount: null,
        activityRule: null,
        antiCheating: null,
        countOfDay: null,
        createUser: null,
        deleteUser: null,
        deleteTime: null,
        modifyUser: null,
        modifyTime: null,
        enterpriseId: null,
        activeStatus: null,
        deleteStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      reductionConfigDialog: false,
    };
  },
  created() {
    this.getList();
  },
  watch: {
    $route() {
      if (this.$route.query.refresh) {
        this.getList(); //重新调用http请求实现页面的重新渲染
      }
    },
  },
  methods: {
    // 开启状态
    updateStatus(row) {
      updateStatus(row.activityId).then((res) => {
        this.getList();
      });
    },
    formatTip(row) {
      let tip = ''
      if(row.cardType && row.cardType.length){
        tip ='电子卡：'+ row.cardType + '\n'
      }
      if(row.physicalCardType && row.physicalCardType.length) {
        tip = tip + ' 实体卡：'+ row.physicalCardType
      }
      return tip
    },
    // 支付方式和会员等级 格式转换
    formatArray(row) {
      const arr = []
      if(row.paymentMethod && row.paymentMethod.length) {
        arr.push (...row.paymentMethod.split(","))
      }
      if (row.cardType && row.cardType.length) {
        arr.push ('电子卡')
      }
      if (row.physicalCardType && row.physicalCardType.length) {
        arr.push ('实体卡')
      }
      return arr.join(",")
    },
    /** 查询立减活动列表 */
    getList() {
      this.loading = true;
      listReductionActivity(this.queryParams).then((response) => {
        this.reductionActivityList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        activityId: null,
        activityName: null,
        activityUse: null,
        activityType: null,
        activityMode: null,
        oilPartOrAll: null,
        memberCondition: null,
        memberLevels: null,
        paymentMethod: null,
        startDate: null,
        endDate: null,
        maxCount: null,
        activityRule: null,
        antiCheating: null,
        countOfDay: null,
        createUser: null,
        createTime: null,
        deleteUser: null,
        deleteTime: null,
        modifyUser: null,
        modifyTime: null,
        enterpriseId: null,
        activeStatus: "0",
        deleteStatus: "0",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.activityId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      /*  this.reset();
        this.open = true;
        this.title = "添加立减活动";*/
      this.$router.push({
        path: "/market/reduction_edit",
        query: { activityId: "" },
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // this.reset();
      // const activityId = row.activityId || this.ids
      // console.log(row);
      this.$router.push({
        path: "/market/reduction_edit",
        query: { activityId: row.activityId, activeStatus: row.activeStatus },
      });
    },
    /** 修改按钮操作 */
    handleEdit(row) {
      if (row.activityEnabled == "1") {
        return this.$message.warning("活动生效中无法编辑");
      }
      this.reset();
      const activityId = row.activityId || this.ids;
      console.log(row);
      this.$router.push({
        path: "/market/reduction_edit",
        query: { activityId: row.activityId, activeStatus: "0" },
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.activityId != null) {
            updateReductionActivity(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addReductionActivity(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const activityIds = row.activityId || this.ids;
      this.$confirm(
        '是否确认删除立减活动编号为"' + activityIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delReductionActivity(activityIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有立减活动数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportReductionActivity(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },

    // 立减配置
    handleReductionConfig() {
      this.reductionConfigDialog = true;
      console.log(this.$store);
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-tooltip__popper.is-light  {
  white-space: pre-line;
}
</style>
